#include <stdlib.h>
#include <avr/io.h>
#include <string.h>

//#include "morse.h"

/* Defines to be later stored in EEPROM, and changeable through serial */
/* DTMF Codes */
#define SHUTDOWN "2570"
#define STARTUP  "2571"

/* Callsign */
#define CALLSIGN "KK6UC"

/* DTMF Code buffer */
uint8_t buffer = 0;

/* DTMF Code string buffer */
char code[5];
int codecounter = 0;

/* function prototypes */
//uint8_t translate(uint8_t);
//void evaluate();



ISR(TIMER1_COMPA_vect)
{
	/* identify every 10 mins */
	sendCode(CALLSIGN, &PORTB, 0x02);
	return 0;
}	
 

/* Evaluate DTMF Code */
void evaluate(void){

	
	if(!strcmp(code, SHUTDOWN)){
		/* turn on red LED, off with Green */
		
		PORTB |= 0x04;
		PORTD &= ~0x80;
	
		/* and... */
		
	}
		
/*	else if(!strcmp(code, STARTUP))*/
		
}

uint8_t translate(uint8_t rec){
	/* returns 0 for 0b1010 DTMF Chip output */
	if(rec == 10)
		return 0;
	
	/* otherwise, return the number */
	else if(rec < 10)
		return rec;
		
	/* or symbol */	

	else if(rec == 11)
		return '*';
	else if(rec == 12)
		return '#';
		
	/* a to d, via adding offset */
	return rec + 53;
		
}

 
int main(void){

	/* enable input on PCx */
	DDRC =  0;
	
	/* enable output for RED, YELLOW LED, and Morse code id osc */
	DDRB |= 0x07;
	
	/* enable output for GREEN LED */
	DDRD |= 0x80;
	
	/* Turn on Green LED */
	PORTD |= 0x80;
	
	while(1){
	

		
		/* test to see if we have DTMF input */
		if( PINC & 0x10 ){
		
			/* read D1, D2, D4, D8 which occupies PINC0 - PINC4 */
			/* discard unwanted bits of PINC5 - PINC7 by shifting around */
			buffer = (PINC << 3) >> 3;
			
			code[codecounter] = translate(buffer);
			
			codecounter ++;
		
		}
		
		if(codecounter >= 4){
			
			/* null terminate string */
			code[4] = '\0';
			
			evaluate();
			codecounter = 0;
			
		}
		
		
	
	
	
	}
	
	



}



